package cate.game.role.res.guild.po.my.mall;

import cate.game.role.Role;
import cate.game.role.mall.refresh.AllRefresh;
import cate.common.table.guild.GuildMallItemRow;
import cate.game.role.res.guild.msg.RefreshGuildMallResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class GuildMallPO extends AllRefresh {
    @NoteField(value = "商店ID")
    public int id;

    @NoteField(value = "商品列表")
    public List<GuildMallItem> itemList;

    @JsonIgnore
    public GuildMallItemRow getRow() {
        return role.getGame().table.guild.mallItem.get(id);
    }

    public GuildMallPO(){}

    public GuildMallPO(Role role, int mallId) {
        super.initialize(role);
        this.id = mallId;
        this.itemList = new ArrayList<>();
        // 首先尝试获取默认的商品列表
        List<GuildMallItemRow> rowList = role.getGame().table.guild.mallItem.getMallItemList(id);
        if (!rowList.isEmpty()) {
            for (GuildMallItemRow row : rowList) {
                itemList.add(new GuildMallItem(role, row.id));
            }
        } else {
            role.getGame().notice.message(role,"没有商品信息");
        }
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (itemList == null) {
            itemList = new ArrayList<>();
        }
        checkItemList();
    }

    public void onDaySpan() {
        // 检查商店的刷新周期
        checkRefreshTime();
        // 检查单个商品的刷新周期
        for (GuildMallItem item : itemList) {
            item.onDaySpan();
        }
    }

    /**
     * 检查商品列表
     */
    private void checkItemList() {
        itemList.removeIf(item -> getRow(item.tid) == null);
        refreshItems();
        // 全部初始化
        for (GuildMallItem item : itemList) {
            item.initialize(role);
        }
    }
    public GuildMallItemRow getRow(int tid) {
        return role.getGame().table.guild.mallItem.get(tid);
    }
    /**
     * 刷新商品列表(全部)
     */
    public void refreshItems() {
        List<GuildMallItemRow> rowList = new ArrayList<>(role.getGame().table.guild.mallItem.getMallItemList(id));
        for (GuildMallItemRow row : rowList) {
            if (itemList.stream().noneMatch(item -> item.tid == row.id)) {
                itemList.add(new GuildMallItem(role, row.id));
            }
        }
    }

    /**
     * 获取商店中的一个商品
     */
    @JsonIgnore
    public GuildMallItem getItem(String idHex) {
        for (GuildMallItem item : itemList) {
            if (StringUtils.equals(item.uid, idHex)) {
                return item;
            }
        }
        return null;
    }

    /**
     * 检查商店的各种刷新时间
     */
    @Override
    @JsonIgnore
    public void checkRefreshTime() {
        // 限购重置周期检测
        if (super.checkAutoCycle(role.getGame())) {
            this.refreshItems();
        }
        // 免费刷新周期检测
        super.checkFreeCycle();
        // 有偿刷新周期检测
        super.checkPayCycle(role.getGame());
    }

    @Override
    public String getAutoCycle() {
        return null;
    }

    @Override
    public String getFreeCycle() {
        return "";
    }

    @Override
    public int getFreeTimeMax() {
        return 0;
    }

    @Override
    public String getPayCycle() {
        return "";
    }

    @Override
    public int getPayTimeMax() {
        return 0;
    }


    /**
     * 通知前端商店信息变更
     */
    public void noticeUpdate() {
        role.sendNow(new RefreshGuildMallResp(this));
    }

    @Override
    public String toString() {
        return "GuildMallPO{" + "id=" + id + ", itemList=" + itemList + '}';
    }

}
